{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "sentence = ['i like dog','i love milk','i hate coffee']\n",
    "words = ' '.join(sentence).split()\n",
    "words = list(set(words))\n",
    "word_num = {w:i for i,w in enumerate(words)}\n",
    "num_word = {i:w for i,w in enumerate(words)}\n",
    "\n",
    "n_class = len(word_num)\n",
    "n_step = 2\n",
    "n_hidden = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_batch(sentence):\n",
    "    input_batch = []\n",
    "    targets = []\n",
    "    for i in range(len(sentence)):\n",
    "        input_s = [word_num[word] for word in sentence[i].split()[:-1]]\n",
    "        target = word_num[sentence[i].split()[-1]]\n",
    "        input_batch.append(np.eye(n_class)[input_s])\n",
    "        targets.append(np.eye(n_class)[target])\n",
    "    return input_batch,targets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cost:1.686907\n",
      "cost:1.431428\n",
      "cost:1.220248\n",
      "cost:1.038802\n",
      "cost:0.891862\n",
      "cost:0.775178\n",
      "cost:0.672690\n",
      "cost:0.578900\n",
      "cost:0.495657\n",
      "cost:0.423640\n",
      "cost:0.362037\n",
      "cost:0.309626\n",
      "cost:0.265213\n",
      "cost:0.227784\n",
      "cost:0.196524\n",
      "cost:0.170554\n",
      "cost:0.148942\n",
      "cost:0.130874\n",
      "cost:0.115687\n",
      "cost:0.102846\n",
      "cost:0.091921\n",
      "cost:0.082565\n",
      "cost:0.074501\n",
      "cost:0.067509\n",
      "cost:0.061409\n",
      "cost:0.056058\n",
      "cost:0.051338\n",
      "cost:0.047153\n",
      "cost:0.043427\n",
      "cost:0.040093\n",
      "cost:0.037098\n",
      "cost:0.034398\n",
      "cost:0.031955\n",
      "cost:0.029737\n",
      "cost:0.027718\n",
      "cost:0.025873\n",
      "cost:0.024184\n",
      "cost:0.022634\n",
      "cost:0.021207\n",
      "cost:0.019892\n",
      "cost:0.018677\n",
      "cost:0.017552\n",
      "cost:0.016509\n",
      "cost:0.015540\n",
      "cost:0.014639\n",
      "cost:0.013799\n",
      "cost:0.013016\n",
      "cost:0.012284\n",
      "cost:0.011600\n",
      "cost:0.010960\n",
      "[0 1 2]\n",
      "[['i', 'like'], ['i', 'love'], ['i', 'hate']] -> ['dog', 'milk', 'coffee']\n"
     ]
    }
   ],
   "source": [
    "X = tf.placeholder(tf.float32,[None,n_step,n_class])#batch_size,n_step,n_hidden\n",
    "Y = tf.placeholder(tf.float32,[None,n_class])#batch_size,n_class\n",
    "\n",
    "input_X = tf.reshape(X,[-1,n_step*n_class])#batch_size,n_step*n_hidden\n",
    "H = tf.Variable(tf.random_normal([n_step*n_class,n_hidden]))\n",
    "d = tf.Variable(tf.random_normal([n_hidden]))\n",
    "U = tf.Variable(tf.random_normal([n_hidden,n_class]))\n",
    "b = tf.Variable(tf.random_normal([n_class]))\n",
    "\n",
    "a = tf.nn.tanh(tf.matmul(input_X,H)+d)\n",
    "y = tf.matmul(a,U)+b#batch_size,n_class\n",
    "\n",
    "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y,labels=Y))\n",
    "optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)\n",
    "prediction = tf.argmax(y,1)\n",
    "\n",
    "input_batch,targets = get_batch(sentence)\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for i in range(5000):\n",
    "        _,loss = sess.run([optimizer,cost],feed_dict = {X:input_batch,Y:targets})\n",
    "        if (i+1)%100 == 0:\n",
    "            print('cost:%.6f' % loss)\n",
    "    predict = sess.run(prediction,feed_dict={X:input_batch})\n",
    "    print([sen.split()[:2] for sen in sentence] ,'->', [num_word[num] for num in predict])\n",
    "                       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
